package com.l2jserver.gameserver.instancemanager.statistics;

import java.util.logging.Logger;

import com.l2jserver.Config;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.cache.HtmCache;
import com.l2jserver.gameserver.model.L2World;

/**
 * @author fissban
 */
public class Statistics
{
	public static Logger _log = Logger.getLogger(Statistics.class.getName());
	private static final int Reuse = Config.STATISTICS_TIME * 1000;
	
	public Statistics()
	{
		if (Config.STATISTICS_GS)
		{
			_log.info("[ Statistics ] Enable");
			ThreadPoolManager.getInstance().scheduleGeneralAtFixedRate(new StatisticsGS(), Reuse, Reuse);
		}
		else
		{
			_log.info("[ Statistics ] Disable");
		}
		
	}
	
	public class StatisticsGS implements Runnable
	{
		@Override
		public void run()
		{
			final long freeMem = ((Runtime.getRuntime().maxMemory() - Runtime.getRuntime().totalMemory()) + Runtime.getRuntime().freeMemory()) / 1048576;
			final long totalMem = Runtime.getRuntime().maxMemory() / 1048576;
			
			_log.info("------------------ Statistics GameServer -------------");
			_log.info("                                                      ");
			_log.info("Users online : " + L2World.getInstance().getAllPlayersCount() + " out of " + Config.MAXIMUM_ONLINE_USERS);
			_log.info("Gameserver free memory " + freeMem + " Mb out of " + totalMem + " Mb");
			_log.info("Threads: " + Thread.activeCount());
			_log.info("Cache[HTML]: " + String.format("%.3f", HtmCache.getInstance().getMemoryUsage()) + " megabytes");
			_log.info("                                                      ");
			_log.info("------------------------------------------------------");
		}
	}
	
	@SuppressWarnings("synthetic-access")
	public static Statistics getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private static class SingletonHolder
	{
		private static final Statistics _instance = new Statistics();
	}
}